package com.smartcruise.task;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.util.network.TaskUpload;
import com.smartcruise.util.network.callBack.BaseCallBack;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetStatusUtils;
import com.smartcruise.util.network.netUtils.Result;

/**
 * Created by FengChaoQun
 * on 2018/3/4 0004
 */

public class TaskDetailsPresenterImp implements TaskDetailsContract.Presenter {

    private TaskDetailsContract.View mView;
    private CruiseData cruiseData;

    public TaskDetailsPresenterImp(TaskDetailsContract.View mView, CruiseData cruiseData) {
        this.mView = mView;
        this.cruiseData = cruiseData;
    }

    @Override
    public void start() {

    }

    @Override
    public void clickOnUploadAll() {
        if (cruiseData.checkContent()) {
            uploadData();
        } else {
            final NormalDialog normalDialog = new NormalDialog(mView.getActivity());
            normalDialog.title("信息不完善")
                    .content("存在节点任务数据不完善,是否继续上传?");
            normalDialog.btnText("上传", "继续完善");
            normalDialog.setOnBtnClickL(new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    uploadData();
                    normalDialog.dismiss();
                }
            }, new OnBtnClickL() {
                @Override
                public void onBtnClick() {
                    normalDialog.dismiss();
                }
            });
            normalDialog.show();
        }
    }

    private void uploadData() {
        if (NetStatusUtils.isConnected(mView.getActivity())) {
            if (NetStatusUtils.isWifi(mView.getActivity())) {//wifi环境下 全部上传
                uploadData(true);
            } else {//不是WiFi的时候 如果有大文件要上传 提醒用户 让其选择
                if (cruiseData.isHaveBigLocalFile()) {
                    final NormalDialog normalDialog = new NormalDialog(mView.getActivity());
                    normalDialog.content("当前使用的是数据流量,上传全部文件会消耗较多流量,是否仅上传概要数据?")
                            .btnText("上传全部数据", "上传概要数据")
                            .setOnBtnClickL(new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    uploadData(true);
                                    normalDialog.dismiss();
                                }
                            }, new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    uploadData(false);
                                    normalDialog.dismiss();
                                }
                            });
                    normalDialog.show();
                } else {
                    uploadData(true);
                }
            }
        } else {
            mView.showToast("网络异常,请检查网络状况");
        }
    }

    private void uploadData(final boolean all) {
        TaskUpload taskUpload = new TaskUpload(cruiseData, mView);
        BaseCallBack baseCallBack = new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {
                mView.showToast("上传成功");
            }
        };
        if (all) {
            taskUpload.uploadAll(baseCallBack);
        } else {
            taskUpload.uploadSimple(baseCallBack);
        }

    }
}
